#include <bits/stdc++.h>

using namespace std;

class Solution {
private:
    vector<vector<int>> bitsList;

public:
    int totalHammingDistance(vector<int> &nums) {
        int N = nums.size();
        bitsList.assign(N + 1, vector<int>(32, 0));
        for (int i = 0; i < N; ++i) {
            auto num = nums[i];
            for (int j = 0; j < 32; ++j) {
                bitsList[i + 1][j] = bitsList[i][j] + (num & 1);
                num >>= 1;
            }
        }
        int totalDist = 0;
        for (int i = 1; i <= N; ++i) {
            for (int j = 0; j < 32; ++j) {
                auto thisBit = bitsList[i][j] - bitsList[i - 1][j];
                auto totalOne = bitsList[N][j] - bitsList[i][j];
                totalDist += thisBit ? N - i - totalOne : totalOne;
            }
        }
        return totalDist;
    }
};

int main() {
    Solution solution;
    vector<int> nums = {4, 14, 2};
    cout << solution.totalHammingDistance(nums) << endl;
}
